

clear all
clc

% b = str2double(getenv('SLURM_ARRAY_TASK_ID'));

datsize = 25;
v       = 23;
start   = 2;
normalization    = 1;
S       = 200;
B       = 100;
out     = 1;

% BASELINE

b   = v;
cf  = 0;

load(['FILEPATH/DataCF' num2str(datsize) '_sim' num2str(b) '_small_endqual_cf' num2str(cf) '_v' num2str(v) '.mat'])

FirmMatchStack  = kron(FirmMatchCF',ones(J,1));
InAndOut0       = [data(:,1:4) varNORM FirmMatchStack];
Share0          = [FirmShare0 Share_cf];

InAndOut0_obs = [data0(:,1:4) varNORM0];

dlmwrite('InAndOut0.csv',InAndOut0,'delimiter', ',', 'precision', 12);
dlmwrite('InAndOut0_obs.csv',InAndOut0_obs,'delimiter', ',', 'precision', 12);
dlmwrite('Share0.csv',Share0,'delimiter', ',', 'precision', 12);

% COUNTERFACTUAL X

cf  = X;
v   = 2;
b   = 1;
datsize = 100;

load(['FILEPATH/DataCF' num2str(datsize) '_sim' num2str(b) '_small_endqual_cf' num2str(cf) '_v' num2str(v) '.mat'])
load('FILEPATH/Match_EndShare_minmax_small.mat')

% Data samples
if b == 1
    sample = 13;
    keep = find(data(:,sample) == 1);
    data = data(keep,:);
    keep = find(data0(:,sample) == 1);
    data0 = data0(keep,:);
    clear keep
end

FirmMatchStack  = kron(FirmMatchCF',ones(J,1));
InAndOut_cf   = [data(:,1:4) ParcelIndex FirmMatrix FirmMatchStack];
Share_cf      = [FirmShare0 Share_cf];

InAndOut_cf_obs = [data0(:,1:4) ParcelIndex0 FirmMatrix0 FirmMatchCF' mean(ParcelUtil_sim,1)' mean(FirmUtil_sim,1)' mean(Quality_sim,1)' ];

InAndOut_notMatched = squeeze(mean(ParcelNotMatched_sim,1));

dlmwrite(['InAndOut_cf' num2str(cf) '_obs.csv'],InAndOut_cf_obs,'delimiter', ',', 'precision', 12);
dlmwrite(['InAndOut_notMatched_cf' num2str(cf) '_obs.csv'],InAndOut_notMatched,'delimiter', ',', 'precision', 12);
dlmwrite(['Share_cf' num2str(cf) '.csv'],Share_cf,'delimiter', ',', 'precision', 12);


FirmMatchStack  = kron(FirmMatchCF',ones(J,1));
InAndOut_cf3   = [data(:,1:4) varNORM FirmMatchStack];
Share_cf3      = [FirmShare0 Share_cf];

InAndOut_cf3_obs = [data0(:,1:4) varNORM0];

dlmwrite('InAndOut_cf3.csv',InAndOut_cf3,'delimiter', ',', 'precision', 12);
dlmwrite('InAndOut_cf3_obs.csv',InAndOut_cf3_obs,'delimiter', ',', 'precision', 12);
dlmwrite('Share_cf3.csv',Share_cf3,'delimiter', ',', 'precision', 12);


